package com.lgq.cfw.service.impl;

import com.lgq.cfw.bean.TableUserRole;
import com.lgq.cfw.bean.example.TableUserRoleExample;
import com.lgq.cfw.bean.example.TableUserRoleExample.Criteria;
import com.lgq.cfw.dao.TableUserRoleMapper;
import com.lgq.cfw.service.RoleAndUserService;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author Lin
 * @date 2019-02-09 15:31
 **/
@Service
public class RoleAndUserServiceImpl implements RoleAndUserService {

  @Autowired
  TableUserRoleMapper tableUserRoleMapper;

  /**
   * 保存用户角色信息
   * @param userId 用户id
   * @param selectedId 被选中的角色id字符串
   */
  @Override
  public void save(Integer userId, String selectedId) {
    TableUserRole tableUserRole=new TableUserRole();
    tableUserRole.setUserid(userId);
    String[] id = selectedId.split("-");
    for (String roleId : id) {
      Integer idInteger = Integer.parseInt(roleId);
      tableUserRole.setRoleid(idInteger);
      tableUserRoleMapper.insertSelective(tableUserRole);
    }
  }


  /**
   * 删除用户角色信息
   * @param userId 用户id
   * @param selectedId 被选中要删除的角色id字符串
   */
  @Override
  public void delete(Integer userId, String selectedId) {
    TableUserRoleExample tableUserRoleExample=new TableUserRoleExample();
    Criteria criteria=tableUserRoleExample.createCriteria();
    List<Integer> roleIdList=new ArrayList<>();
    String[] id = selectedId.split("-");
    for (String roleId : id) {
      Integer idInteger = Integer.parseInt(roleId);
      roleIdList.add(idInteger);
    }
    criteria.andUseridEqualTo(userId);
    criteria.andRoleidIn(roleIdList);
    tableUserRoleMapper.deleteByExample(tableUserRoleExample);
  }
}
